//*****************************************
//	Oscar Rodriguez Polo	c) 2007
//
//	UTestBaseClass.cpp
//
//	UNITARY TEST Base Class for NANOSAT1B
//
//*****************************************


#include <ut_base/UTBase.h>

#include <public/basic_types.h>
#include <public/debug_primitives.h>
#include <public/stdlib.h>

CUTBase * CUTBase::sp_firstTest=NULL;
CUTBase * CUTBase::sp_lastTest=NULL;

static bool_t SetSeed;

//Ojo solo usarlo cuando la plataforma posix. en la plataforma ercos tiene que haber una forma equivalente de obtener la semilla.
//TODO extern "C" unsigned __random_seed;

//********************************************************************
// Constructor

CUTBase::CUTBase(const char * id,
				bool_t hasPriority,
				bool_t setSeed,
				int32_t seed)
{

	mp_ID=id;
	mp_next=NULL;
	SetSeed = setSeed;
	         
	if(sp_firstTest==NULL)
	{
		sp_firstTest=this;
	    sp_lastTest=this;
        mp_prev=NULL;
	}
	else
	{
		if(hasPriority)
		{
			mp_next=sp_firstTest;
			sp_firstTest->mp_prev=this;
			sp_firstTest=this;
	    	mp_prev=NULL;
		
		}
		else
		{
    		mp_prev=sp_lastTest;
	    	sp_lastTest->mp_next=this;
        	sp_lastTest=this;
		}
	}

	if (setSeed)
	{
		m_seed=seed;
	}

}

//********************************************************************
// Destructor

CUTBase::~CUTBase()
{

	if(mp_next)
	{
		mp_next->mp_prev=mp_prev;
	}
	else
	{
    	sp_lastTest=mp_prev;
	}

	if(mp_prev)
	{
		mp_prev->mp_next=mp_next;
	}
	else
	{
		sp_firstTest=mp_next;
	}

}

//********************************************************************
// Destructor

void CUTBase::ConfigTest()
{

	if (SetSeed)
	{
		srand(m_seed);
	}
	else
	{
	//TODO	m_seed=__random_seed;
	}
}

//********************************************************************

bool_t CUTBase::CompareInt8(const int8_t & waitedVal
								, const int8_t & val
								, const char * errorMsg)
{
	bool_t testOK=true;

	if(waitedVal!=val)
	{
		testOK=false;
		//DEBUG("%s. Waited Value: %d. Value %d.", errorMsg, waitedVal, val);
	}			

	return testOK;
}

bool_t CUTBase::CompareInt16(const int16_t & waitedVal
								, const int16_t & val
								, const char * errorMsg)
{
	bool_t testOK=true;

	if(waitedVal!=val)
	{
		testOK=false;
		//DEBUG("%s. Waited Value: %d. Value %d.", errorMsg, waitedVal, val);
	}			

	return testOK;
}

bool_t CUTBase::CompareInt32(const int32_t & waitedVal
								, const int32_t & val
								, const char * errorMsg)
{
	bool_t testOK=true;

	if(waitedVal!=val)
	{
		testOK=false;
		//DEBUG("%s. Waited Value: %d. Value %d.", errorMsg, waitedVal, val);
	}			

	return testOK;
}

bool_t CUTBase::CompareInt64(const int64_t & waitedVal
								, const int64_t & val
								, const char * errorMsg)
{
	bool_t testOK=true;

	if(waitedVal!=val)
	{
		testOK=false;
		//DEBUG("%s: Waited Value: %lld  Value %lld", errorMsg, waitedVal, val);
	}			

	return testOK;
}

bool_t CUTBase::CompareUInt8(const uint8_t & waitedVal
								, const uint8_t & val
								, const char * errorMsg)
{
	bool_t testOK=true;

	if(waitedVal!=val)
	{
		testOK=false;
		//DEBUG("%s. Waited Value: %u. Value %u.", errorMsg, waitedVal, val);
	}			

	return testOK;
}

bool_t CUTBase::CompareUInt16(const uint16_t & waitedVal
								 , const uint16_t & val
								 , const char * errorMsg)
{
	bool_t testOK=true;

	if(waitedVal!=val)
	{
		testOK=false;
		//DEBUG("%s. Waited Value: %u. Value %u.", errorMsg, waitedVal, val);
	}			

	return testOK;
}

bool_t CUTBase::CompareUInt32(const uint32_t & waitedVal
								 , const uint32_t & val
								 , const char * errorMsg)
{
	bool_t testOK=true;

	if(waitedVal!=val)
	{
		testOK=false;
		//DEBUG("%s. Waited Value: %u. Value %u.", errorMsg, waitedVal, val);
	}			

	return testOK;
}

bool_t CUTBase::CompareUInt64(const uint64_t & waitedVal
								 , const uint64_t & val
								 , const char * errorMsg)
{
	bool_t testOK=true;

	if(waitedVal!=val)
	{
		testOK=false;
		//DEBUG("%s. Waited Value: %llu. Value %llu.", errorMsg, waitedVal, val);
	}			

	return testOK;
}

// CompareByte

bool_t CUTBase::CompareByte(const byte_t & waitedVal,
								const byte_t & val,
								const char * errorMsg)
{
	bool_t testOK=true;

	if(waitedVal!=val)
	{
		testOK=false;
		//DEBUG("%s. Waited Value: %x. Value %x.", errorMsg, waitedVal, val);
	}

	return testOK;
}


bool_t CUTBase::CompareWord16(const word16_t & waitedVal
								 , const word16_t & val
								 , const char * errorMsg)
{
	bool_t testOK=true;

	if(waitedVal!=val)
	{
		testOK=false;
		//DEBUG("%s. Waited Value: %x. Value %x.", errorMsg, waitedVal, val);
	}			
	
	return testOK;
}

bool_t CUTBase::CompareWord32(const word32_t & waitedVal
							, const word32_t & val
							, const char * errorMsg)
{
	bool_t testOK=true;

	if(waitedVal!=val)
	{
		testOK=false;
		//DEBUG("%s. Waited Value: %x. Value %x.", errorMsg, waitedVal, val);
	}			

	return testOK;
}

bool_t CUTBase::CompareFloat(const float & waitedVal
							, const float & val
							, const char * errorMsg)
{
	bool_t testOK=true;

	if(waitedVal!=val)
	{
		testOK=false;
		//DEBUG("%s. Waited Value: %f. Value %f.", errorMsg, waitedVal, val);
	}			

	return testOK;
}
